var Constant = require('utils/constant.js');
App({
  userLogin : function (that = this) {
    console.log('user login');
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: Constant.BASE_URL + '/api/user/login',
          data: {
            code: res.code
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
          },
          method: 'POST',
          success: function (res) {

            wx.setStorageSync('authCode', res.data.authCode)

            wx.getUserInfo({
              success: info_res => {
                console.log(info_res)
                that.globalData.userInfo = info_res.userInfo

                if (res.data.needMoreInfo == true) {
                  wx.request({
                    url: Constant.BASE_URL + '/api/user/decodeUserInfo',
                    data: {
                      authCode: wx.getStorageSync('authCode'),
                      iv: info_res.iv,
                      encryptedData: info_res.encryptedData,
                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
                    },
                    method: 'POST',
                    success: function (decode_res) {
                      if (that.globalData.is_develop) {
                        console.log('---code 换取用户信息2 ---');
                        console.log(decode_res);
                      }
                    }
                  })
                }
              }
            })

          }
        })
      }
    })
  },

  onLaunch: function () {
    console.log('on launch');
    var that = this
    // 展示本地存储能力

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })    
    let authcode = wx.getStorageSync('authCode');
    console.log(authcode);
    if(authcode==''){
      that.userLogin();
    }
    wx.checkSession({
      success :function(res){
        console.log(res)
      
      },
      fail:res=>{
        // 登录
        that.userLogin();
      }
    })
   
  },
  /**
   * 请求数据
   * @param that Page的对象，用其进行数据的更新
   */
  requestData: function (url, formData, method, success, fail) {
    var that = this
    if (that.globalData.is_develop) {
      console.log('url=' + url)
      console.log('formData=' + formData)
      console.log('method=' + method)
    }

    wx.request({
      url: Constant.BASE_URL + url,
      data: formData,
      method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
      },
      success: function (res) {
        if (that.globalData.is_develop) {
          console.log('requestData-success-res')
          console.log(res)
        }
        if (res.errMsg == 'request:ok') {
          typeof success == "function" && success(res.data);
        } else {
          typeof fail == "function" && fail(res);
        }
      },
      fail: function (res) {
        if (that.globalData.is_develop) {
          console.log('requestData-fail-res')
          console.log(res);
        }
        typeof fail == "function" && fail(res);
      }
    });
  },
  globalData: {
    userInfo: null,
    is_develop: false
  }
})